home *** CD-ROM | disk | FTP | other *** search
/ The Best of MacTutor - S…e Code for Volumes 1 to 5 / The Best of MacTutor - Source Code for Volume 1-5 (Wayzata Technology)(6031)(1990).bin / Source Code / #45 (Jun 89) / Supe SubCode / MiniEdit.c < prev    next >
C/C++ Source or Header  |  1989-02-19  |  10KB  |  517 lines

  1. /**********************************************************************************
  2.  
  3.     MiniEdit.c
  4.     
  5. ***********************************************************************************/
  6.  
  7. /* NEEDED TO DEFINE NEW TEXTEDIT ROUTINES IN TEXTEDIT.H */
  8. #define __ALLNU__
  9.  
  10. #include <QuickDraw.h>
  11. #include <Memory.h>
  12. #include <OSEvents.h>
  13. #include <Types.h>
  14. #include <Fonts.h>
  15. #include <Windows.h>
  16. #include <Menus.h>
  17. #include <TextEdit.h>
  18. #include <Dialogs.h>
  19. #include <Events.h>
  20. #include <Desk.h>
  21. #include <Files.h>
  22. #include <ToolUtils.h>
  23. #include <Controls.h>
  24. #include <resources.h>
  25. #include <strings.h>
  26. #include <Scrap.h>
  27.  
  28. #include "MiniEdit.h"
  29. #include "TrapHook.h"
  30.  
  31. #define                DrawCharNum    0x085
  32. #define                GetRes        0x1A0
  33.  
  34. WindowRecord        wRecord;
  35. WindowPtr            myWindow;
  36. TEHandle                TEH;
  37. Rect                    dragRect = { 0, 0, 1024, 1024 };
  38. MenuHandle            myMenus[3];
  39. ControlHandle         vScroll;
  40. Cursor                editCursor;
  41. Cursor                waitCursor;
  42. char                    dirty;
  43. EventRecord            myEvent;
  44. int                    baseoffset = 0;
  45. TrapHookPointer    InsertHookAddr;
  46. TrapHookPointer    ResHook;
  47. long                    NormalTrap;
  48. long                    ResTrap;
  49.  
  50. int                    currFontItem,currSizeItem;
  51.  
  52. GetDrawTrapInfo();
  53. GetResTrapInfo();
  54. DrawTextHook();
  55.  
  56. /* ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ */
  57.  
  58. main() 
  59. {
  60.     InitGraf(&qd.thePort);
  61.     InitFonts();
  62.     FlushEvents( everyEvent, 0 );
  63.     InitWindows();
  64.     InitMenus();
  65.     TEInit();
  66.     InitDialogs(0L);
  67.     InitCursor();
  68.     MaxApplZone();
  69.     
  70.     SetUpCursors();
  71.     SetUpMenus();
  72.     SetUpWindows();
  73.     
  74.     SetupHook(GetDrawTrapInfo,DrawTextHook,DrawCharNum,&InsertHookAddr,&NormalTrap);
  75.     SetupHook(GetResTrapInfo,NULL,GetRes,&ResHook,&ResTrap);
  76.     
  77.         while (MainEvent());
  78.     
  79.     UnsetTrapHook(InsertHookAddr);
  80.     UnsetTrapHook(ResHook);
  81. }
  82.  
  83. /* ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ */
  84.  
  85. doMouse(myEvent)
  86.     EventRecord     *myEvent;
  87. {    
  88.     WindowPtr        whichWindow;
  89.     
  90.     switch (FindWindow( myEvent->where, &whichWindow ))
  91.     {
  92.         case inDesk: 
  93.             break;
  94.         case inGoAway:
  95.             if (ours(whichWindow))
  96.                 if (TrackGoAway( myWindow, myEvent->where) )
  97.                 {    // CloseMyWindow();
  98.                 }
  99.             break;
  100.         case inMenuBar:
  101.             return( DoCommand( MenuSelect(myEvent->where) ) );
  102.         case inSysWindow:
  103.             SystemClick( myEvent, whichWindow );
  104.             break;
  105.         case inDrag:
  106.             if (ours(whichWindow))
  107.                 DragWindow( whichWindow, myEvent->where, &dragRect );
  108.             break;
  109.         case inGrow:
  110.             if (ours(whichWindow))
  111.                 MyGrowWindow( whichWindow, myEvent->where );
  112.             break;
  113.         case inContent:
  114.             if (whichWindow != FrontWindow())
  115.                 SelectWindow(whichWindow);
  116.             else 
  117.                 if (ours(whichWindow))
  118.                     DoContent(whichWindow, myEvent);
  119.             break;
  120.         default: ;
  121.     }
  122.     
  123.     return(1);
  124. }
  125.  
  126. /* ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ */
  127.  
  128. doKey(myEvent)
  129.     EventRecord        *myEvent;
  130. {
  131.     register char    theChar;
  132.     
  133.     theChar = myEvent->message & charCodeMask;
  134.     if ((myEvent->modifiers & cmdKey) != 0) 
  135.         return( DoCommand( MenuKey( theChar ) ));
  136.     else
  137.     {
  138.         TEKey( theChar, TEH );
  139.         ShowSelect();
  140.     }
  141.     
  142.     return(1);
  143. }
  144.  
  145. /* ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ */
  146.  
  147. doActivate(myEvent)
  148.     EventRecord        *myEvent;
  149. {
  150.     Rect                rct;
  151.  
  152.     if (ours((WindowPtr)myEvent->message))
  153.     {
  154.         myWindow        = (WindowPtr)myEvent->message;
  155.         
  156.         rct            = (*myWindow).portRect;
  157.         rct.top         = rct.bottom - (SBarWidth+1);
  158.         rct.left        = rct.left - (SBarWidth+1);
  159.         InvalRect(&rct);
  160.         
  161.         if ( myEvent->modifiers & activeFlag )
  162.         {
  163.             TEActivate( TEH );
  164.             ShowControl( vScroll );
  165.             TEFromScrap();
  166.         }
  167.         else
  168.         {
  169.             TEDeactivate(TEH);
  170.             HideControl( vScroll );
  171.             ZeroScrap();
  172.             TEToScrap();
  173.         }
  174.     }
  175.     
  176.     return(1);
  177. }
  178.  
  179. /* ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ */
  180.  
  181. int MainEvent() 
  182. {
  183.     MaintainCursor();
  184.     MaintainMenus();
  185.     
  186.     SetTrapAddress(NormalTrap,DrawCharNum);
  187.     SetTrapAddress(ResTrap,GetRes);
  188.     
  189.         SystemTask();
  190.         
  191.     SetTrapAddress((long)&(InsertHookAddr->Code),DrawCharNum);
  192.     SetTrapAddress((long)&(ResHook->Code),GetRes);
  193.     
  194.     TEIdle(TEH);
  195.     if (GetNextEvent(everyEvent, &myEvent))
  196.     {
  197.         switch (myEvent.what)
  198.         {    case mouseDown:
  199.                 return(doMouse(&myEvent));
  200.                 break;
  201.             case keyDown:
  202.             case autoKey: 
  203.                 return(doKey(&myEvent));
  204.                 break;
  205.             case activateEvt:
  206.                 return(doActivate(&myEvent));
  207.                 break;
  208.             case updateEvt: 
  209.                 if (ours((WindowPtr)myEvent.message))
  210.                     UpdateWindow(myWindow);
  211.                 break;
  212.             default: ;
  213.         }
  214.     }
  215.     
  216.     return(1);
  217. }
  218.  
  219. /* ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ */
  220.  
  221. SetUpMenus()
  222. {
  223.     int        i;
  224.     
  225.     myMenus[appleM] = GetMenu(appleID);
  226.     myMenus[fileM]  = GetMenu(fileID);
  227.     myMenus[editM]  = GetMenu(editID);
  228.     
  229.     AddResMenu( myMenus[appleM], 'DRVR' );
  230.     
  231.     for ( (i=appleM); (i<=editM); i++ ) InsertMenu(myMenus[i], 0) ;
  232.     
  233.     CheckItem(myMenus[2],currFontItem=NewYorkCommand,1);
  234.     CheckItem(myMenus[2],currSizeItem=s14Command,1);
  235.     
  236.     DrawMenuBar();
  237. }
  238.  
  239. /* ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ */
  240.  
  241. doScrapEditing(theItem)
  242.     int            theItem;
  243. {
  244.     switch (theItem)
  245.     {    case cutCommand:
  246.             TECut( TEH );
  247.             break;
  248.         case copyCommand:
  249.             TECopy( TEH );
  250.             break;
  251.         case pasteCommand:
  252.             TEPaste( TEH );
  253.             break;
  254.     }
  255. }    
  256.  
  257. /* ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ */
  258.  
  259. doFontEditing(theItem)
  260.     int            theItem;
  261. {
  262.     TextStyle    Styl;
  263.     
  264.     CheckItem(myMenus[2],currFontItem,0);
  265.     currFontItem = theItem;
  266.     CheckItem(myMenus[2],currFontItem,1);
  267.  
  268.     switch (theItem)
  269.     {    case TimesCommand:
  270.             Styl.tsFont = times;
  271.             break;
  272.         case GenevaCommand:
  273.             Styl.tsFont = geneva;
  274.             break;
  275.         case NewYorkCommand:
  276.             Styl.tsFont = newYork;
  277.             break;
  278.     }    
  279.     TESetStyle(doFont,&Styl,1,TEH);
  280. }
  281.  
  282. /* ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ */
  283.  
  284. doSizeEditing(theItem)
  285.     int            theItem;
  286. {
  287.     TextStyle    Styl;
  288.     
  289.     CheckItem(myMenus[2],currSizeItem,0);
  290.     currSizeItem = theItem;
  291.     CheckItem(myMenus[2],currSizeItem,1);
  292.  
  293.     switch (theItem)
  294.     {    case s9Command:
  295.             Styl.tsSize = 9;
  296.             break;
  297.         case s10Command:
  298.             Styl.tsSize = 10;
  299.             break;
  300.         case s12Command:
  301.             Styl.tsSize = 12;
  302.             break;
  303.         case s14Command:
  304.             Styl.tsSize = 14;
  305.             break;
  306.         case s18Command:
  307.             Styl.tsSize = 18;
  308.             break;
  309.     }
  310.     TESetStyle(doSize,&Styl,1,TEH);
  311. }
  312.  
  313. /* ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ */
  314.  
  315. doFaceEditing(theItem)
  316.     int            theItem;
  317. {
  318.     TextStyle    Styl;
  319.     short            lhgt,ascent;
  320.     
  321.     switch (theItem)
  322.     {    case plainCommand:
  323.             Styl.tsFace = 0;
  324.             break;
  325.         case boldCommand:
  326.             TEGetStyle((**TEH).selStart,&Styl,&lhgt,&ascent,TEH);
  327.             Styl.tsFace |= bold;
  328.             break;
  329.         case ulineCommand:
  330.             TEGetStyle((**TEH).selStart,&Styl,&lhgt,&ascent,TEH);
  331.             Styl.tsFace |= underline;
  332.             break;
  333.         case italicCommand:
  334.             TEGetStyle((**TEH).selStart,&Styl,&lhgt,&ascent,TEH);
  335.             Styl.tsFace |= italic;
  336.             break;
  337.         case shadowCommand:
  338.             TEGetStyle((**TEH).selStart,&Styl,&lhgt,&ascent,TEH);
  339.             Styl.tsFace |= shadow;
  340.             break;
  341.     }
  342.     TESetStyle(doFace,&Styl,1,TEH);
  343. }
  344.  
  345. /* ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ */
  346.  
  347. doScriptEditing(theItem)
  348.     int            theItem;
  349. {
  350.     TextStyle    Styl;
  351.     short            lhgt,ascent;
  352.     
  353.     switch (theItem)
  354.     {    case SuperCommand:
  355.             TEGetStyle((**TEH).selStart-1,&Styl,&lhgt,&ascent,TEH);
  356.             Styl.tsFont &= REGULAR;
  357.             Styl.tsFont |= SUPER;
  358.             break;
  359.         case RegularCommand:
  360.             TEGetStyle((**TEH).selStart-1,&Styl,&lhgt,&ascent,TEH);
  361.             Styl.tsFont &= REGULAR;
  362.             break;
  363.         case SubCommand:
  364.             TEGetStyle((**TEH).selStart-1,&Styl,&lhgt,&ascent,TEH);
  365.             Styl.tsFont &= REGULAR;
  366.             Styl.tsFont |= SUB;
  367.             break;
  368.     }
  369.     TESetStyle(doFont,&Styl,1,TEH);
  370. }
  371.  
  372. /* ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ */
  373.  
  374. doEditing(theItem)
  375.     int            theItem;
  376. {
  377.     switch (theItem)
  378.     {    case cutCommand:
  379.         case copyCommand:
  380.         case pasteCommand:
  381.             doScrapEditing(theItem);
  382.             break;
  383.         
  384.         case TimesCommand:
  385.         case GenevaCommand:
  386.         case NewYorkCommand:
  387.             doFontEditing(theItem);
  388.             break;
  389.             
  390.         case s9Command:
  391.         case s10Command:
  392.         case s12Command:
  393.         case s14Command:
  394.         case s18Command:
  395.             doSizeEditing(theItem);
  396.             break;
  397.             
  398.         case plainCommand:
  399.         case boldCommand:
  400.         case ulineCommand:
  401.         case italicCommand:
  402.         case shadowCommand:
  403.             doFaceEditing(theItem);
  404.             break;
  405.             
  406.         case SuperCommand:
  407.         case RegularCommand:
  408.         case SubCommand:
  409.             doScriptEditing(theItem);
  410.             break;
  411.             
  412.         default: ;
  413.     }
  414.     ShowSelect();
  415. }
  416.  
  417. /* ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ */
  418.  
  419. int DoCommand( mResult )
  420.     long mResult;
  421. {
  422.     int            theItem;
  423.     Str255        name;
  424.     
  425.     theItem = LoWord( mResult );
  426.     switch (HiWord(mResult))
  427.     {    case appleID:
  428.             GetItem(myMenus[appleM], theItem, &name);
  429.             OpenDeskAcc( &name );
  430.             SetPort( myWindow );
  431.             break;
  432.         case fileID:
  433.             if (theItem == fmQuit) return(0);
  434.             break;
  435.         case editID: 
  436.             if (SystemEdit(theItem-1)==0)
  437.             doEditing(theItem);
  438.             break;
  439.     }
  440.     HiliteMenu(0);
  441.     return(1);
  442. }
  443.  
  444. /* ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ */
  445.  
  446. MaintainCursor()
  447. {
  448.     Point            pt;
  449.     WindowPeek    wPtr;
  450.     GrafPtr        savePort;
  451.     Rect            TERect;
  452.     
  453.     wPtr    =    (WindowPeek)FrontWindow();
  454.     
  455.     if (ours(wPtr))
  456.     {    GetPort( &savePort );
  457.         SetPort( (GrafPtr)wPtr );
  458.         
  459.         GetMouse(&pt);
  460.         TERect = (**TEH).viewRect;
  461.  
  462.         if ( PtInRect(pt, &TERect)  )
  463.             SetCursor( &editCursor);
  464.         else 
  465.             InitCursor();
  466.         
  467.         SetPort( savePort );
  468.     }
  469. }
  470.  
  471. /* ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ */
  472.  
  473. MaintainMenus()
  474. {
  475.     if ( !(*(WindowPeek)myWindow).visible || 
  476.             !ours(FrontWindow()) )
  477.     {
  478.         EnableItem( myMenus[editM], cutCommand );
  479.         EnableItem( myMenus[editM], copyCommand );
  480.     }
  481.     else
  482.     {
  483.         if ((**TEH).selStart==(**TEH).selEnd) {
  484.             DisableItem( myMenus[editM], cutCommand );
  485.             DisableItem( myMenus[editM], copyCommand );
  486.         }
  487.         else
  488.         {
  489.             EnableItem( myMenus[editM], cutCommand );
  490.             EnableItem( myMenus[editM], copyCommand );
  491.         }
  492.     }
  493. }
  494.  
  495. /* ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ */
  496.  
  497. SetUpCursors()
  498. {
  499.     CursHandle    hCurs;
  500.     
  501.     hCurs         = GetCursor(1);
  502.     editCursor     = **hCurs;
  503.     hCurs         = GetCursor(watchCursor);
  504.     waitCursor     = **hCurs;
  505. }
  506.  
  507. /* ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ */
  508.  
  509. ours(w)
  510.     WindowPtr w;
  511. {
  512.     return( (myWindow!=NULL) && (w==myWindow) );
  513. }
  514.  
  515. /* ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ */
  516.  
  517.